using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Xml;
namespace Microsoft.Phone.Controls.Maps.Core
{
	internal class MapConfigurationFromWeb : MapConfigurationProvider
	{
		private readonly object configLock = new object();
		private readonly Uri configurationUri;
		private Collection<MapConfigurationGetSectionRequest> requestQueue;
		public override event EventHandler<MapConfigurationLoadedEventArgs> Loaded
		{
			[MethodImpl(32)]
			add
			{
				this.Loaded = (EventHandler<MapConfigurationLoadedEventArgs>)Delegate.Combine(this.Loaded, value);
			}
			[MethodImpl(32)]
			remove
			{
				this.Loaded = (EventHandler<MapConfigurationLoadedEventArgs>)Delegate.Remove(this.Loaded, value);
			}
		}
		public MapConfigurationFromWeb(Uri configurationUri)
		{
			this.configurationUri = configurationUri;
		}
		public override void LoadConfiguration()
		{
			try
			{
				this.requestQueue = new Collection<MapConfigurationGetSectionRequest>();
				WebClient webClient = new WebClient();
				webClient.add_OpenReadCompleted(new OpenReadCompletedEventHandler(this.LoadFromServer_OpenReadCompleted));
				webClient.OpenReadAsync(this.configurationUri);
			}
			catch (Exception error)
			{
				if (this.Loaded != null)
				{
					this.Loaded.Invoke(this, new MapConfigurationLoadedEventArgs(error));
				}
			}
		}
		public override void GetConfigurationSection(string version, string sectionName, string culture, MapConfigurationCallback callback, object userState)
		{
			bool flag;
			if (!(flag = (base.Sections != null)))
			{
				object obj;
				Monitor.Enter(obj = this.configLock);
				try
				{
					if (!(flag = (base.Sections != null)))
					{
						this.requestQueue.Add(new MapConfigurationGetSectionRequest(version, sectionName, culture, callback, userState));
					}
				}
				finally
				{
					Monitor.Exit(obj);
				}
			}
			if (flag && callback != null)
			{
				callback(base.GetSection(version, sectionName, culture), userState);
			}
		}
		private void LoadFromServer_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
		{
			try
			{
				if (e.get_Error() != null || e.get_Result() == null)
				{
					throw e.get_Error();
				}
				XmlReader sectionReader = XmlReader.Create(e.get_Result());
				Dictionary<string, MapConfigurationSection> sections = MapConfigurationProvider.ParseConfiguration(sectionReader);
				object obj;
				Monitor.Enter(obj = this.configLock);
				try
				{
					base.Sections = sections;
				}
				finally
				{
					Monitor.Exit(obj);
				}
				if (this.Loaded != null)
				{
					this.Loaded.Invoke(this, new MapConfigurationLoadedEventArgs(null));
				}
			}
			catch (Exception error)
			{
				object obj2;
				Monitor.Enter(obj2 = this.configLock);
				try
				{
					base.Sections = new Dictionary<string, MapConfigurationSection>();
				}
				finally
				{
					Monitor.Exit(obj2);
				}
				if (this.Loaded != null)
				{
					this.Loaded.Invoke(this, new MapConfigurationLoadedEventArgs(error));
				}
			}
			using (IEnumerator<MapConfigurationGetSectionRequest> enumerator = this.requestQueue.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					MapConfigurationGetSectionRequest current = enumerator.get_Current();
					if (current.Callback != null)
					{
						current.Callback(base.GetSection(current.Version, current.SectionName, current.Culture), current.UserState);
					}
				}
			}
			this.requestQueue = null;
		}
	}
}
